package com.google.code.teamtools.gwtrpc.mock;

import com.google.code.teamtools.client.model.IResource;
import com.google.code.teamtools.client.model.IResourceOwner;
import com.google.code.teamtools.client.service.TeamService;
import com.google.code.teamtools.client.model.Team;
import com.google.code.teamtools.client.model.TeamMember;
import java.util.ArrayList;
import java.util.List;

public class MockTeamServiceImpl implements TeamService
{
    private static Long id = 0L;
    
    private static Team root = new Team(id++, "Team A");

    private static List<IResource> teams;
    static
    {
        teams = new ArrayList<IResource>();
        teams.add(new Team(id++, "Team B", root));
        teams.add(new Team(id++, "Team C", root));
    }

    private static List<IResource> teamAMembers;
    static
    {
        teamAMembers = new ArrayList<IResource>();
        teamAMembers.add(new TeamMember(id++, "Dave", (IResourceOwner) teams.get(0)));
        teamAMembers.add(new TeamMember(id++, "John", (IResourceOwner) teams.get(0)));
        teamAMembers.add(new TeamMember(id++, "Anne", (IResourceOwner) teams.get(0)));
    }

    private static List<IResource> teamBMembers;
    static
    {
        teamBMembers = new ArrayList<IResource>();
        teamBMembers.add(new TeamMember(id++, "Mike", (IResourceOwner) teams.get(1)));
        teamBMembers.add(new TeamMember(id++, "Sally", (IResourceOwner) teams.get(1)));
        teamBMembers.add(new TeamMember(id++, "James", (IResourceOwner) teams.get(1)));
        teamBMembers.add(new TeamMember(id++, "Phil", (IResourceOwner) teams.get(1)));
    }

    public Team getResourceRoot(String cookieValue)
    {
        return root;
    }

    public List<IResource> getResourceChildren(String cookieValue, IResourceOwner owner)
    {
        List<IResource> children = new ArrayList<IResource>();

        // If no owner is given simply return the root of the resource heirarchy.
        if (owner == null) {
            children.add(getResourceRoot(cookieValue));
            return children;
        }

        if (owner.getId().equals(root.getId()))
            return teams;
        else if (owner.getId().equals(teams.get(0).getId()))
            return teamAMembers;
        else if (owner.getId().equals(teams.get(1).getId()))
            return teamBMembers;
        else
            return children;    // intentionally empty
    }
}
